<template>
    <div>
        <div class="background">
            <section class="header">
                <video autoplay loop class="void-background" muted plays-inlne>
                    <source src="../mv/G2s.mp4" type="video/mp4"/>
                    浏览器不支持 video 标签，建议升级浏览器。
                </video>
            </section>
        </div>
        <!--            <el-card class="card">-->
        <div class="card">
            <div>
                <h2>登 录</h2>
                <br><br>
            </div>
            <el-form :model="user" ref="loginForm" :rules="loginRule" :hide-required-asterisk="true" inline>
                <el-form-item label="账号" prop="account">
                    <el-input v-model="user.account" style="width: 210px;"></el-input>
                </el-form-item>
                <br>
                <el-form-item label="密码" prop="password">
                    <!--     @keypress.enter.native 按下回车键，调用登录方法     -->
                    <el-input v-model="user.password" @keypress.enter.native="login" show-password
                              style="width: 210px;"></el-input>
                </el-form-item>
                <br>
                <el-form-item>
                    <el-button style="border-radius: 10px;" @click="login">登录</el-button>
                    <el-button style="color: white;" type="text" @click="$router.push('/register')">没有账号？去注册</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!--            </el-card>-->
    </div>
</template>
<script>

    export default {
        name: '',
        data() {
            return {
                input1: '',
                input2: '',
                user: {},
                loginRule: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
                    ]
                },
                redirect: this.$route.query.redirect
            }
        },
        methods: {
            login() {
                // 前端表单验证
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        // 表单验证通过后，调用后端接口
                        this.$axios.post('/api/user/login', this.user).then(res => {
                            console.log(res)
                            if (res.data.status === 1) {
                                // 登录成功，把token放到本地缓存
                                localStorage.setItem('TOKEN', res.data.data.token);
                                // 把用户名也存起来
                                localStorage.setItem('USER_NAME', res.data.data.name);
                                localStorage.setItem('USER_IMG', res.data.data.img);
                                // 如果是从某个页面调到登录页的，就返回这个页面，不然返回后台首页
                                this.$router.push(this.redirect || '/index');
                                this.$message.success(res.data.msg);
                            } else {
                                // this.$router.push(this.redirect || '/admin/index');
                                this.$message.error(res.data.msg);
                            }
                        })
                    } else {
                        this.$message.error('请填写完整内容');
                        return false;
                    }
                })
            }
        }
    }
</script>
<style scoped>
    @import "../css/login.css";
</style>


